"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 创建非A库，创建whale插件，合理报错
Create At   : 2023/11/21
Owner       : @zou_jialiang050
Description :
    1.创建B、C、PG兼容性数据库
    2.分别在三个库中创建whale插件
Expect      :
    1.创建成功
    2.插件创建失败
History     :
"""
import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname_b = 'db_whale_02_b'
        self.dbname_c = 'db_whale_02_c'
        self.dbname_pg = 'db_whale_02_pg'
    
    def test_gms_alert(self):
        text = '---step1:创建B兼容性数据库;expect:成功---'
        self.logger.info(text)
        sql_cmd1 = f"create database {self.dbname_b} dbcompatibility='B';" \
            f"create database {self.dbname_c} dbcompatibility='C';" \
            f"create database {self.dbname_pg} dbcompatibility='PG';"
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(msg1)
        self.assertEqual(msg1.count(self.constant.CREATE_DATABASE_SUCCESS), 3,
                         text + '执行失败')
        
        text = '---step2:分别在三个库中创建whale插件;expect:失败---'
        self.logger.info(text)
        sql_cmd2 = f"create extension whale;"
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname_b)
        self.logger.info(msg2)
        msg3 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname_c)
        self.logger.info(msg3)
        msg4 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname_pg)
        self.logger.info(msg4)
        error_msg = 'ERROR:  please create extension "whale" with A ' \
                    'type DBCOMPATIBILITY'
        self.assertTrue(msg2 == msg3 == msg4 == error_msg, text + '执行失败')
    
    def tearDown(self):
        text = '---清理环境---'
        clean_cmd = f"drop database {self.dbname_b};" \
            f"drop database {self.dbname_c};" \
            f"drop database {self.dbname_pg};"
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertEqual(clean_msg.count(self.constant.DROP_DATABASE_SUCCESS),
                         3, text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
